import {
	IAbility,
	IContext,
	IPanelDetailAbility,
	IPanelDetailController,
	IPanelDetailControllerParams,
	IParam,
} from '@/core/interface';
import { LayoutUtil } from '@/core/utils';

/**
 * 面板成员控制器基类
 *
 * @export
 * @class PanelDetailController
 * @implements {IPanelDetailController}
 */
export class PanelDetailController implements IPanelDetailController {
	/**
	 * 类型 视图布局/项布局
	 *
	 * @type {('VIEWLAYOUT' | 'ITEMLAYOUT')}
	 * @memberof PanelDetailController
	 */
	public type: 'VIEWLAYOUT' | 'ITEMLAYOUT' = 'VIEWLAYOUT';

	/**
	 * 成员标题
	 *
	 * @type {string}
	 * @memberof PanelDetailController
	 */
	public caption = '';

	/**
	 * 视图类型
	 *
	 * @protected
	 * @type {string}
	 * @memberof PanelDetailController
	 */
	protected readonly viewType: string;

	/**
	 * 成员类型
	 *
	 * @type {string}
	 * @memberof PanelDetailController
	 */
	public itemType = '';

	/**
	 * 成员样式
	 *
	 * @type {string}
	 * @memberof PanelDetailController
	 */
	public itemStyle = '';

	/**
	 * 面板对象
	 *
	 * @type {*}
	 * @memberof PanelDetailController
	 */
	protected panel: any = null;

	/**
	 * 成员名称
	 *
	 * @type {string}
	 * @memberof PanelDetailController
	 */
	public name = '';

	/**
	 * 成员是否显示
	 *
	 * @type {boolean}
	 * @memberof PanelDetailController
	 */
	public visible = true;

	/**
	 * 当前数据
	 *
	 * @type {*}
	 * @memberof PanelDetailController
	 */
	protected data: any;

	/**
	 * 布局模式
	 *
	 * @type {string}
	 * @memberof PanelDetailController
	 */
	public layout = '';

	/**
	 *  布局占位（边缘布局使用）
	 *
	 * @type {string}
	 * @memberof PanelDetailController
	 */
	public layoutPos = '';

	/**
	 * 布局高度
	 *
	 * @type {number}
	 * @memberof PanelDetailController
	 */
	public layoutHeight = 0;

	/**
	 * 高度模式
	 *
	 * @type {string}
	 * @memberof PanelDetailController
	 */
	public heightMode = '';

	/**
	 * 布局宽度
	 *
	 * @type {number}
	 * @memberof PanelDetailController
	 */
	public layoutWidth = 0;

	/**
	 * 宽度模式
	 *
	 * @type {string}
	 * @memberof PanelDetailController
	 */
	public widthMode = '';

	/**
	 * 下档间隔
	 *
	 * @type {string}
	 * @memberof PanelDetailController
	 */
	public spacingBottom = '';

	/**
	 * 左侧间隔
	 *
	 * @type {string}
	 * @memberof PanelDetailController
	 */
	public spacingLeft = '';

	/**
	 * 右侧间隔
	 *
	 * @type {string}
	 * @memberof PanelDetailController
	 */
	public spacingRight = '';

	/**
	 * 顶部间隔
	 *
	 * @type {string}
	 * @memberof PanelDetailController
	 */
	public spacingTop = '';

	/**
	 * 自身水平对齐模式
	 *
	 * @type {string}
	 * @memberof PanelDetailController
	 */
	public hAlignSelf = '';

	/**
	 * 自身垂直对齐模式
	 *
	 * @type {string}
	 * @memberof PanelDetailController
	 */
	public vAlignSelf = '';

	/**
	 * Flex延伸
	 *
	 * @type {number}
	 * @memberof PanelDetailController
	 */
	public flexGrow = 0;

	/**
	 * flex布局参数
	 *
	 * @type {*}
	 * @memberof PanelDetailController
	 */
	public flexParams: any = {};

	/**
	 * 是否显示标题
	 *
	 * @type {boolean}
	 * @memberof PanelDetailController
	 */
	public isShowCaption = false;

	/**
	 * 界面样式表
	 *
	 * @type {string}
	 * @memberof PanelDetailController
	 */
	public sysCss = '';

	/**
	 * @description 图标
	 * @type {*}
	 * @memberof PanelDetailController
	 */
	public sysImage: any = {};

	/**
	 * 预置类型
	 *
	 * @type {string}
	 * @memberof PanelDetailController
	 */
	protected predefinedType = '';

	/**
	 * 下标
	 *
	 * @type {number}
	 * @memberof PanelDetailController
	 */
	protected index = 0;

	/**
	 * 父项名称
	 *
	 * @type {string | undefined}
	 * @memberof PanelDetailController
	 */
	public parentName: string | undefined = undefined;

	/**
	 * 成员模型数据
	 *
	 * @protected
	 * @type {IParam}
	 * @memberof PanelDetailController
	 */
	protected readonly model: IParam = {};

	/**
	 * 成员能力
	 *
	 * @protected
	 * @type {IPanelDetailAbility}
	 * @memberof PanelDetailController
	 */
	protected ability!: IPanelDetailAbility;

	/**
	 * Creates an instance of PanelDetailController.
	 * @param {IPanelDetailControllerParams} params
	 * @memberof PanelDetailController
	 */
	constructor(params: IPanelDetailControllerParams) {
		this.model = params;
		this.type = params.type;
		this.caption = params.caption;
		this.itemType = params.itemType;
		this.panel = params.panel;
		this.name = params.name;
		this.visible = params.visible;
		this.layout = params.layout;
		this.layoutPos = params.layoutPos;
		this.layoutHeight = params.layoutHeight;
		this.heightMode = params.heightMode;
		this.layoutWidth = params.layoutWidth;
		this.widthMode = params.widthMode;
		this.spacingBottom = params.spacingBottom;
		this.spacingLeft = params.spacingLeft;
		this.spacingRight = params.spacingRight;
		this.spacingTop = params.spacingTop;
		this.hAlignSelf = params.hAlignSelf;
		this.vAlignSelf = params.vAlignSelf;
		this.flexGrow = params.flexGrow;
		this.flexParams = params.flexParams;
		this.isShowCaption = params.isShowCaption;
		this.sysCss = params.sysCss;
		this.predefinedType = params.predefinedType;
		this.itemStyle = params.itemStyle;
		this.sysImage = params.sysImage;
		this.parentName = params.parentName;
		this.viewType = params.viewType;
		this.detailInit();
	}

	/**
	 * 成员初始化
	 *
	 * @protected
	 * @memberof PanelDetailController
	 */
	protected detailInit() {
		this.visible = this.model.visible || true;
		if (this.model.groupLogics) {
			const panelVisible = this.model.groupLogics.find(
				(logic: IParam) => logic.logicCat === 'PANELVISIBLE'
			);
			if (panelVisible) {
				this.visible = false;
			}
		}
	}

	/**
	 * 获取能力
	 *
	 * @return {*}  {IAbility}
	 * @memberof PanelDetailController
	 */
	getAbility(): IAbility {
		return {
			name: this.name,
			getModel: this.getModel.bind(this),
			controller: this as any,
			viewCtx: this.panel.store.viewCtx,
		};
	}

	/**
	 * 获取模型
	 *
	 * @template T
	 * @return {*}  {T}
	 * @memberof PanelDetailController
	 */
	public getModel<T>(): T {
		return this.model as T;
	}

	/**
	 * 设置数据
	 *
	 * @param {*} value
	 * @memberof PanelDetailController
	 */
	public setData(value: any) {
		this.data = value;
	}

	/**
	 * 获取数据
	 *
	 * @return {*}
	 * @memberof PanelDetailController
	 */
	public getData() {
		return this.data;
	}

	/**
	 * 设置当前下标
	 *
	 * @param {number} index
	 * @memberof PanelDetailController
	 */
	public setIndex(index: number) {
		this.index = index;
	}

	/**
	 * 获取当前下标
	 *
	 * @return {*}
	 * @memberof PanelDetailController
	 */
	public getIndex() {
		return this.index;
	}

	/**
	 * 获取元素样式（除容器之外的元素，包含内容盒子 大小/边距/内容 的样式）
	 *
	 * @memberof PanelDetailController
	 */
	public getElementStyle() {
		const elementStyle = {};
		Object.assign(elementStyle, this.getBoxSizeStyle());
		Object.assign(elementStyle, this.getBoxSpacingStyle());
		Object.assign(elementStyle, this.getBoxSelfAlignStyle());
		return elementStyle;
	}

	/**
	 * 获取盒子间隔样式（元素）
	 *
	 * @memberof PanelDetailController
	 */
	protected getBoxSpacingStyle() {
		const boxStyle = {};
		// 上方间隔模式
		if (this.spacingTop) {
			Object.assign(
				boxStyle,
				LayoutUtil.getItemSpacingStyle(this.spacingTop, 'top')
			);
		}
		// 下方间隔模式
		if (this.spacingBottom) {
			Object.assign(
				boxStyle,
				LayoutUtil.getItemSpacingStyle(this.spacingBottom, 'bottom')
			);
		}
		// 左侧间隔模式
		if (this.spacingLeft) {
			Object.assign(
				boxStyle,
				LayoutUtil.getItemSpacingStyle(this.spacingLeft, 'left')
			);
		}
		// 右侧间隔模式
		if (this.spacingRight) {
			Object.assign(
				boxStyle,
				LayoutUtil.getItemSpacingStyle(this.spacingRight, 'right')
			);
		}
		return boxStyle;
	}

	/**
	 * 识别flex布局方向，横轴对齐，纵轴对齐（元素）
	 *
	 * @return {*}
	 * @memberof PanelDetailController
	 */
	protected getFlexStyle() {
		const boxStyle = {};
		if (
			this.flexParams.align ||
			this.flexParams.dir ||
			this.flexParams.vAlign
		) {
			Object.assign(boxStyle, { display: 'flex' });
			if (this.flexParams.dir) {
				Object.assign(boxStyle, { 'flex-direction': this.flexParams.dir });
			}
			if (this.flexParams.align) {
				Object.assign(boxStyle, { 'justify-content': this.flexParams.align });
			}
			if (this.flexParams.vAlign) {
				Object.assign(boxStyle, { 'align-items': this.flexParams.vAlign });
			}
		}
		return boxStyle;
	}

	/**
	 * 获取盒子大小样式（元素）
	 *
	 * @memberof PanelDetailController
	 */
	protected getBoxSizeStyle() {
		const boxStyle = {};
		if (this.widthMode || this.layoutWidth) {
			Object.assign(
				boxStyle,
				LayoutUtil.getBoxSize('WIDTH', this.widthMode, this.layoutWidth)
			);
		}
		if (this.heightMode || this.layoutHeight) {
			Object.assign(
				boxStyle,
				LayoutUtil.getBoxSize('HEIGHT', this.heightMode, this.layoutHeight)
			);
		}
		return boxStyle;
	}

	/**
	 * 获取自身对齐模式(元素)
	 *
	 * @memberof PanelDetailController
	 */
	protected getBoxSelfAlignStyle() {
		const boxStyle = {};
		// 自身对齐方式
		if (this.vAlignSelf || this.hAlignSelf) {
			// 自身垂直对齐模式
			switch (this.vAlignSelf) {
				case 'TOP':
					// 默认样式
					break;
				case 'MIDDLE':
					Object.assign(boxStyle, { margin: 'auto 0px' });
					break;
				case 'BOTTOM':
					Object.assign(boxStyle, { 'margin-top': 'auto' });
					break;
				default:
					break;
			}
			// 自身水平对齐模式
			switch (this.hAlignSelf) {
				case 'LEFT':
					Object.assign(boxStyle, { 'align-self': 'flex-start' });
					break;
				case 'CENTER':
					Object.assign(boxStyle, { 'align-self': 'center' });
					break;
				case 'RIGHT':
					Object.assign(boxStyle, { 'align-self': 'flex-end' });
					break;
				case 'JUSTIFY':
					// 默认样式
					break;
				default:
					break;
			}
		}
		return boxStyle;
	}

	/**
	 * 获取盒子布局样式(布局,包含约束内容区布局的样式)
	 *
	 * @memberof PanelDetailController
	 */
	protected getBoxLayOutStyle() {
		const boxStyle = {};
		if (this.parentName && this.panel) {
			const parentItem = this.panel.store.layoutModelDetails[this.parentName];
			// 识别父FLEX
			if (parentItem) {
				if (parentItem.layout == 'FLEX') {
					Object.assign(boxStyle, {
						'flex-grow': this.flexGrow ? this.flexGrow : 0,
					});
				}
				// 识别SIMPLEFLEX
				else if (parentItem.layout == 'SIMPLEFLEX') {
					// SIMPLEFLEX 按照内容撑大小
					Object.assign(boxStyle, { 'flex-grow': 0 });
					if (this.flexGrow !== -1) {
						Object.assign(boxStyle, {
							width: `${(100 / 12) * this.flexGrow}%`,
							height: '100%',
						});
					} else {
						// 简单FLEX布局自适应
						Object.assign(boxStyle, {
							'flex-grow': 1,
							'min-width': `${100 / 12}%`,
							height: '100%',
						});
					}
				} else {
					// 当前项兄弟项个数大于1且本身设置宽或者高默认不占满
					if (
						parentItem &&
						parentItem.details &&
						parentItem.details.length > 1
					) {
						const boxSizeStyle: any = this.getBoxSizeStyle();
						if (boxSizeStyle && Object.keys(boxSizeStyle).length > 0) {
							Object.assign(boxStyle, { 'flex-grow': 0 });
						}
					}
				}
			}
		}
		if (!this.visible) {
			Object.assign(boxStyle, { display: 'none' });
		}
		return boxStyle;
	}

	/**
	 * 加载
	 * @param context
	 * @param viewParams
	 */
	public async load(context: IContext, viewParams: IParam) {}
}
